<template>
  <Page>
    <!-- 搜索组件 -->
    <el-card :bordered="false" class="mb-3">
      <ProSearchGroup
        isReset
        :options="searchOptions"
        :selectProps="{ style: { width: '160px' }, value: searchValue }"
        :inputProps="{ placeholder: '请输入名称' }"
        @submit="searchGroupSubmit"
      />
    </el-card>

    <!-- 表格组件 -->
    <el-card :bordered="false" class="mt-3 proCard">
      <Table
        :columns="columns"
        :request="loadDataTable"
        row-key="id"
        ref="tableRef"
        :actionColumn="actionColumn"
      >
        <template #tableTitle>
          <!-- 新增按钮 -->
          <el-button type="primary" @click="addUser">
            <el-icon class="mr-1">
              <PlusOutlined />
            </el-icon>
            新增角色
          </el-button>
        </template>
      </Table>
    </el-card>

    <!-- 模态框组件 -->
    <modal ref="modalRef" :title="createModalTitle" :isEdit="isEdit" />
  </Page>
</template>

<script lang="ts" setup>
  import { nextTick, reactive, ref, unref, h } from 'vue';
  import { ElMessage } from 'element-plus';
  import { BasicColumn, Table, TableAction } from '@/design/components/Table';
  import api from '@/api/system/role';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import modal from './modal.vue';
  import { ProSearchGroup } from '@/components/ProSearchGroup';

  // 响应式变量
  const tableRef = ref(); // 表格组件的引用
  const modalRef = ref(); // 模态框组件的引用

  const createModalTitle = ref('添加用户'); // 模态框标题，默认为"添加用户"
  const isEdit = ref(false); // 是否处于编辑状态，默认为非编辑状态

  const searchValue = ref('name'); // 搜索关键字的值

  // 表格请求参数
  const params = ref({
    searchKey: 'name',
    searchValue: '',
  });

  // 搜索选项
  const searchOptions = [
    {
      label: '名称',
      value: 'name',
    },
  ];

  /**
   * 操作选型
   */
  const actionColumn: BasicColumn = reactive({
    width: 300,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    render({ row: record }) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
            type: 'text',
          },
          {
            label: '删除',
            isConfirm: true,
            type: 'text',
            popConfirm: {
              title: '您确定要删除吗？',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              onConfirm: handleDelete.bind(null, record),
            },
          },
        ],
      });
    },
  });

  // 加载表格数据的请求函数
  const loadDataTable = async (res: any) => {
    let _params = {
      ...unref(params),
      ...res,
    };
    return await api.list(_params);
  };

  // 搜索组件提交事件处理函数
  function searchGroupSubmit(res) {
    params.value.searchKey = res.searchKey;
    params.value.searchValue = res.searchValue;
    reloadTable();
  }

  // 添加用户
  function addUser() {
    isEdit.value = false;
    modalRef.value.setProps({ title: '添加用户' });
    modalRef.value.openModal();
  }

  // 重新加载表格数据
  function reloadTable() {
    tableRef.value.reload();
  }

  // 编辑操作
  function handleEdit(record: Recordable) {
    record.mobile = parseInt(record.mobile);
    addUser();
    nextTick(() => {
      isEdit.value = true;
      modalRef.value.setProps({ title: '编辑用户' });
      modalRef.value.setFieldsValue({
        ...unref(record),
      });
    });
  }

  function handleDelete(record: Recordable) {
    console.log('🚀 ~ handleDelete ~ record:', record);
    return;
    /*deleteRole(record.id).then(() => {
      ElMessage.success('删除成功');
      reloadTable();
    });*/
  }
</script>

<style lang="scss" scoped></style>
